package com.yygo.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tiansha on 2015/7/4.
 */

@Entity
@Table(name = "rule")
public class Rule extends BaseObject {

    private static final long serialVersionUID = -8493388034221769146L;
    private Long id;
    private String code;
    private String description;

    private String url;
    private List<String> numbers = new ArrayList<>();

    private LoanRequest loanRequest;

    public Rule() {}
    public Rule(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length = 4)
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @ManyToOne()
    @JoinColumn(name="req_id")
    public LoanRequest getLoanRequest() {
        return loanRequest;
    }

    public void setLoanRequest(LoanRequest loanRequest) {
        this.loanRequest = loanRequest;
    }

    @ElementCollection(targetClass=java.lang.String.class)
    @JoinTable(
            name="rule_number",
            joinColumns = {@JoinColumn(name="rule_id")}
    )
    @Column(name="number")
    public List<String> getNumbers() {
        return numbers;
    }

    public void setNumbers(List<String> numbers) {
        this.numbers = numbers;
    }

    @Column(length = 10)
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return code + ": " + description;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Bank)) {
            return false;
        }

        final Rule rule = (Rule) o;

        return !(id != null ? !id.equals(rule.getId()) : rule.getId() != null);
    }

    @Override
    public int hashCode() {
        return id.intValue();
    }
}
